/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.intec.matsab.dao;

import edu.intec.matsab.persistence.Asignatura;
import edu.intec.matsab.persistence.PersistenceManager;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceException;
import javax.persistence.Query;

/**
 *
 * @author matilde
 */
public class AsignaturaDAO extends BasicoDAO {


    public static Asignatura buscarPorIdAsignatura(String idAsignatura) {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager manager = emf.createEntityManager();

        Asignatura asignatura = new Asignatura();

        try {
            Query query = manager.createNamedQuery("Asignatura.findByIdAsignatura");
            query.setParameter("idAsignatura", idAsignatura);
            asignatura = (Asignatura) query.getSingleResult();

        } catch (PersistenceException exception) {
            exception.printStackTrace();
        } finally {
            manager.close();
        }

        return asignatura;
    }


    public static List<Asignatura> getAllAsignaturas() {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager manager = emf.createEntityManager();

        List<Asignatura> listAsignaturas = new ArrayList<Asignatura>();

        try {
            Query query = manager.createNamedQuery("Asignatura.AllAsignaturas");
            listAsignaturas = query.getResultList();

        } catch (PersistenceException exception) {
            exception.printStackTrace();
        } finally {
            manager.close();
        }

        return listAsignaturas;
    }

     public static boolean eliminar(Asignatura obj) {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager manager = emf.createEntityManager();

        boolean resultado = false;

        try {

            manager.getTransaction().begin();
            Asignatura asignatura = manager.getReference(Asignatura.class, obj.getIdAsignatura());

            manager.remove(asignatura);
            manager.getTransaction().commit();

            resultado = true;

        } catch (PersistenceException exception) {
            exception.printStackTrace();
        } finally {
            manager.close();
        }

        return resultado;
    }

    public static List<Asignatura> getAsignaturasPorEstatus(String idEstado) {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager manager = emf.createEntityManager();

        List<Asignatura> listAsignaturas = new ArrayList<Asignatura>();

        try {
            Query query = manager.createNamedQuery("Asignatura.AllAsignaturasByEstado");
            query.setParameter("idEstado", idEstado);
            listAsignaturas = query.getResultList();

        } catch (PersistenceException exception) {
            exception.printStackTrace();
        } finally {
            manager.close();
        }

        return listAsignaturas;
    }
}
